import * as ordersService from '../services/orders';
import { getQuery } from 'utils/helper';

export default {
  namespace: 'orders',
  state: {
    items: [],
    total: 0,
    page: 1,
    customerId: '',
  },
  reducers: {
    save(
      state,
      {
        payload: { items, total, page, customerId },
      }
    ) {
      return { ...state, items, total, page, customerId };
    },
    saveDetail(
      state,
      {
        payload: { orderDetail },
      }
    ) {
      return { ...state, orderDetail };
    },
  },
  effects: {
    *fetchList(
      {
        payload: { page = 1, ...rest },
      },
      { call, put }
    ) {
      const { data } = yield call(ordersService.fetchList, { page, ...rest });
      const { items, total } = data;
      yield put({
        type: 'save',
        payload: {
          ...rest,
          items,
          total,
          page: parseInt(page, 10),
        },
      });
    },
    *fetchRefunds(
      {
        payload: { page = 1, ...rest },
      },
      { call, put }
    ) {
      const { data } = yield call(ordersService.fetchRefunds, { page, ...rest });
      const { items, total } = data;
      yield put({
        type: 'save',
        payload: {
          ...rest,
          items,
          total,
          page: parseInt(page, 10),
        },
      });
    },
    *fetch({ payload: id }, { call, put }) {
      const response = yield call(ordersService.fetch, id);
      const { data: orderDetail } = response;

      yield put({
        type: 'saveDetail',
        payload: {
          orderDetail,
        },
      });

      return response;
    },
    *refund(
      {
        payload: { id, remark },
      },
      { call, put, select }
    ) {
      const response = yield call(ordersService.refund, id, remark);
      const query = getQuery();

      yield put({ type: 'fetchList', payload: query });

      return response;
    },
    *confirmRefund(
      {
        payload: { id },
      },
      { call, put, select }
    ) {
      const response = yield call(ordersService.confirmRefund, id);
      const query = getQuery();

      yield put({ type: 'fetchRefunds', payload: query });

      return response;
    },
    *cancelRefund(
      {
        payload: { id },
      },
      { call, put, select }
    ) {
      const response = yield call(ordersService.cancelRefund, id);
      const page = yield select(state => state.orders.page);

      yield put({ type: 'fetchRefunds', payload: { page } });

      return response;
    },
    // *update(
    //   {
    //     payload: { id, values },
    //   },
    //   { call, put, select }
    // ) {
    //   yield call(ordersService.update, id, values);
    //   const page = yield select(state => state.orders.page);
    //   yield put({ type: 'fetchList', payload: { page } });
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/orders/list') {
          // 查询所有订单列表
          dispatch({ type: 'fetchList', payload: query });
        } else if (pathname === '/orders/refund') {
          // 查询申请退款订单
          dispatch({ type: 'fetchRefunds', payload: query });
        }
      });
    },
  },
};
